<template>
  <div class="login" @keydown.enter="handleSubmit">
    <div class="login-con">
      <Card :bordered="false">
        <p slot="title">
          <Icon type="log-in"></Icon>
          <span style="font-size: 16px;">HRCMS建站系统</span>
        </p>
        <div class="form-con">
          <Form ref="loginForm" :model="form" :rules="rules">
            <FormItem prop="userName">
              <Input size="large" v-model="form.userName" placeholder="请输入用户名">
              <span slot="prepend">
                     <Icon :size="16" type="person"></Icon>
                 </span>
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input size="large" type="password" v-model="form.password" placeholder="请输入密码">
              <span slot="prepend">
                     <Icon :size="14" type="locked"></Icon>
                 </span>
              </Input>
            </FormItem>
            <FormItem prop="captcha">
              <Row>
                <Col span="12">
                  <Input size="large" v-model="form.captcha" placeholder="验证码">
                  <span slot="prepend">
                     <Icon :size="14" type="lock-combination"></Icon>
                   </span>
                  </Input>
                </Col>
                <Col span="12" class="login-captcha">
                  <img :src="captchaPath" @click="getCaptcha" alt="">
                </Col>
              </Row>
            </FormItem>
            <FormItem>
              <Button @click="handleSubmit" type="primary" long>登录</Button>
            </FormItem>
          </Form>
          <!--<p class="login-tip">输入任意用户名和密码即可</p>-->
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie';
import Util from '@/libs/util';
export default {
  data () {
    return {
      form: {
        userName: '',
        password: '',
        uuid: '',
        captcha: ''
      },
      rules: {
        userName: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      },
      captchaPath: ''
    };
  },
  created () {
    this.getCaptcha()
  },
  methods: {
    handleSubmit () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/sys/login'),
            method: 'post',
            data: this.$http.adornData({
              'username': this.form.userName,
              'password': this.form.password,
              'uuid': this.form.uuid,
              'captcha': this.form.captcha
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              Cookies.set('token', data.token, { expires: data.expire / (3600 * 24) || 0 });
              this.$router.replace({ name: 'home' });
            } else {
              this.getCaptcha();
              this.$Message.error(data.msg);
            }
          })
        }
      });
    },
    getCaptcha () { // 获取验证码
      this.form.uuid = Util.getUUID();
      this.captchaPath = this.$http.adornUrl(`/captcha.jpg?uuid=${this.form.uuid}`);
    }
  }
}
</script>

<style lang='less'>
  .login{
    width: 100%;
    height: 100%;
    //background-image: url(../assets/back.jpg);
    //background-size: cover;
    //background-position: center;
    background: rgb(2, 0, 36);
    background: linear-gradient(90deg, rgba(2, 0, 36, 1) 0%, rgba(9, 9, 80, 1) 26%, rgba(0, 212, 255, 1) 100%);
    position: relative;
    &-title{
      text-align: center; /*让div内部文字居中*/
      border-radius: 20px;
      width: 600px;
      margin: auto;
      position: absolute;
      top: 40%;
      left: 10%;
      font-size: 30px;
      letter-spacing:5px;
      color: #dddee1;
    }
    &-con{
      position: absolute;
      right: 160px;
      top: 55%;
      transform: translateY(-60%);
      width: 300px;
      &-header{
        font-size: 16px;
        font-weight: 300;
        text-align: center;
        padding: 30px 0;
      }
      .form-con{
        padding: 10px 0 0;
      }
      .login-tip{
        font-size: 10px;
        text-align: center;
        color: #c3c3c3;
      }
      .login-captcha {
        overflow: hidden;
        >img {
          padding-top: 2px;
          padding-left: 5px;
          width: 100%;
          cursor: pointer;
        }
      }
    }
  }
</style>
